<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Shorturl extends MY_Controller {

    public function __construct() {
        parent::__construct();
        $this->load->model('Url_model');
    }

    public function index() {
        $this->show_view('shorturl');
    }

    function saveUrl() {
        $s = $this->session->all_userdata();
        $tmp = $s['0'];
        $id = $tmp->id;

        $data['user_id'] = $id;
        $data['long_url'] = $this->input->post('long_url');
        $website = $data['long_url'];
        if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i", $website)) {
            $this->session->set_flashdata('msg', 'Invalid URL, Please enter valid URL');
            redirect(base_url().'shorturl');
            //echo "Invalid URL";
        } else {


            $data['short_url'] = substr(md5(uniqid(rand(), true)),0,6);
            $this->Url_model->saveUrl($data);
            $data['getes'] = $this->Url_model->getUrl();

            //$this->show_view('shorturl', $data);
        }
    }

}

/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */